<?php
/**
 * 报量单Service
 */

declare (strict_types = 1);

namespace app\school\service;

use app\common\model\Brand;
use app\common\model\Dose;
use app\common\model\Meal;
use app\common\model\Menu;
use app\common\model\Spec;
use app\common\model\Type;
use app\school\exception\ApiServiceException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class DoseService extends ApiBaseService
{
    protected Dose $model;

    public function __construct()
    {
        $this->model = new Dose();
    }

    /**
     * 列表
     * @param $param
     * @param $page
     * @param $limit
     * @return array
     * @throws ApiServiceException
     */
    public function getList($param, $page, $limit)
    {
        try {
            $field     = 'id,school_id,week,create_time';
            $start_end = getstart();
            $start     = $start_end['start'];
            $end       = $start_end['end'];

            $data = $this->model
                ->with(['school'])
                ->scope('ApiWhere', $param)
                ->where('week', 'between', [$start, $end])
                ->paginate([
                    'list_rows' => $limit,
                    'page'      => $page,
                ])
                ->each(function ($item, $key) {

                    $detail2 = $item['detail2'];
                    foreach ($item['detail2'] as $key => $value) {
                        $detail2[$key]['type'] = Type::where('id', $value['type_id'])->find();

                        $detail2[$key]['brand'] = Brand::where('id', $value['brand_id'])->find();

                        $detail2[$key]['spec'] = Spec::where('id', $value['spec_id'])->find();

                    }
                    $item['detail2'] = $detail2;

                    $detail = $item['detail'];
                    if ($item['tag'] == 0) {
                        // foreach ($item['detail'] as $key => $value) {
                        // 	$meal = Meal::where('id', $value['meal_id'])->find();
                        // 	$meal_list = [];
                        // 	foreach ($meal['detail'] as $mk => $mv) {
                        // 		$meal_list = Menu::where('id', $mv['menu_id'])->value('detail');
                        // 		foreach ($meal_list as $mmk => $mmv) {

                        // 			$meal_list[$mmk]['type'] = Type::where('id', $mmv['type_id'])->find();

                        // 			$meal_list[$mmk]['brand'] = Brand::where('id', $mmv['brand_id'])->find();

                        // 			$meal_list[$mmk]['spec'] = Spec::where('id', $mmv['spec_id'])->find();
                        // 			$meal_list[$mmk]['old_menu'] = Menu::where('id', $mmv['menu_id'])->find();
                        // 			$meal_list[$mmk]['old_menu_id'] = $mmv['menu_id'];
                        // 			$meal_list[$mmk]['menu_name'] = Menu::where('id', $mv['menu_id'])->value('name');
                        // 			$meal_list[$mmk]['menu_id'] = $mv['menu_id'];

                        // 		}
                        // 		$meal['menu'] = $meal_list;
                        // 	}
                        // 	$detail[$key]['meal'] = $meal;
                        // }
                        foreach ($item['detail'] as $key => $value) {
                            $meal = Meal::where('id', $value['meal_id'])->value('detail');
                            if (! empty($meal)) {
                                $meal_id_list = array_column($meal, 'menu_id');
                            } else {
                                $meal_id_list = [];
                            }

                            $meal_list = Menu::whereIn('id', $meal_id_list)->select();

                            foreach ($meal_list as $mmk => $mmv) {
                                $menu_detail = $mmv['detail'];
                                foreach ($mmv['detail'] as $k => $v) {
                                    $menu_detail[$k]['type'] = Type::where('id', $v['type_id'])->find();

                                    $menu_detail[$k]['brand'] = Brand::where('id', $v['brand_id'])->find();

                                    $menu_detail[$k]['spec'] = Spec::where('id', $v['spec_id'])->find();
                                    // $menu_detail[$k]['old_menu'] = Menu::where('id', $v['menu_id'])->find();
                                    // $menu_detail[$k]['old_menu_id'] = $v['menu_id'];

                                }
                                $mmv['detail'] = $menu_detail;
                                //$meal_list[$mmk] = $mmv;

                            }

                            $meals['menu'] = ($meal_list);

                            $detail[$key]['meal'] = $meals;
                        }
                    } else {
                        foreach ($item['detail'] as $key => $value) {
                            $meal         = Meal::where('id', $value['meal_id'])->value('detail');
                            $meal_id_list = array_column($meal, 'menu_id');

                            $meal_list = Menu::whereIn('id', $meal_id_list)->select();

                            foreach ($meal_list as $mmk => $mmv) {
                                $menu_detail = $mmv['detail'];
                                foreach ($mmv['detail'] as $k => $v) {
                                    $menu_detail[$k]['type'] = Type::where('id', $v['type_id'])->find();

                                    $menu_detail[$k]['brand'] = Brand::where('id', $v['brand_id'])->find();

                                    $menu_detail[$k]['spec']        = Spec::where('id', $v['spec_id'])->find();
                                    $menu_detail[$k]['old_menu']    = Menu::where('id', $v['menu_id'])->find();
                                    $menu_detail[$k]['old_menu_id'] = $v['menu_id'];

                                }
                                $mmv['detail'] = $menu_detail;
                                //$meal_list[$mmk] = $mmv;

                            }

                            $meals['menu'] = ($meal_list);

                            $detail[$key]['meal'] = $meals;
                        }
                    }

                    $item['detail'] = $detail;
                    return $item;

                });
        } catch (DataNotFoundException | ModelNotFoundException $e) {
            $data = [];
        } catch (DbException $e) {
            throw new ApiServiceException('查询列表失败，信息' . $e->getMessage());
        }

        return $data;
    }
    public function getList2($param, $page, $limit)
    {
        try {
            $field = 'id,school_id,week,create_time';

            $data = $this->model
                ->with(['school'])
                ->scope('ApiWhere', $param)
                ->paginate([
                    'list_rows' => $limit,
                    'page'      => $page,
                ])
                ->each(function ($item, $key) {

                    $detail2 = $item['detail2'];
                    foreach ($item['detail2'] as $key => $value) {
                        $detail2[$key]['type'] = Type::where('id', $value['type_id'])->find();

                        $detail2[$key]['brand'] = Brand::where('id', $value['brand_id'])->find();

                        $detail2[$key]['spec'] = Spec::where('id', $value['spec_id'])->find();

                    }
                    $item['detail2'] = $detail2;

                    $detail = $item['detail'];
                    if ($item['tag'] == 0) {
                        // foreach ($item['detail'] as $key => $value) {
                        // 	$meal = Meal::where('id', $value['meal_id'])->find();
                        // 	$meal_list = [];
                        // 	foreach ($meal['detail'] as $mk => $mv) {
                        // 		$meal_list = Menu::where('id', $mv['menu_id'])->value('detail');
                        // 		foreach ($meal_list as $mmk => $mmv) {

                        // 			$meal_list[$mmk]['type'] = Type::where('id', $mmv['type_id'])->find();

                        // 			$meal_list[$mmk]['brand'] = Brand::where('id', $mmv['brand_id'])->find();

                        // 			$meal_list[$mmk]['spec'] = Spec::where('id', $mmv['spec_id'])->find();
                        // 			$meal_list[$mmk]['old_menu'] = Menu::where('id', $mmv['menu_id'])->find();
                        // 			$meal_list[$mmk]['old_menu_id'] = $mmv['menu_id'];
                        // 			$meal_list[$mmk]['menu_name'] = Menu::where('id', $mv['menu_id'])->value('name');
                        // 			$meal_list[$mmk]['menu_id'] = $mv['menu_id'];

                        // 		}
                        // 		$meal['menu'] = $meal_list;
                        // 	}
                        // 	$detail[$key]['meal'] = $meal;
                        // }
                        foreach ($item['detail'] as $key => $value) {
                            $meal = Meal::where('id', $value['meal_id'])->value('detail');
                            if (! empty($meal)) {
                                $meal_id_list = array_column($meal, 'menu_id');
                            } else {
                                $meal_id_list = [];
                            }

                            $meal_list = Menu::whereIn('id', $meal_id_list)->select();

                            foreach ($meal_list as $mmk => $mmv) {
                                $menu_detail = $mmv['detail'];
                                foreach ($mmv['detail'] as $k => $v) {
                                    $menu_detail[$k]['type'] = Type::where('id', $v['type_id'])->find();

                                    $menu_detail[$k]['brand'] = Brand::where('id', $v['brand_id'])->find();

                                    $menu_detail[$k]['spec'] = Spec::where('id', $v['spec_id'])->find();
                                    // $menu_detail[$k]['old_menu'] = Menu::where('id', $v['menu_id'])->find();
                                    // $menu_detail[$k]['old_menu_id'] = $v['menu_id'];

                                }
                                $mmv['detail'] = $menu_detail;
                                //$meal_list[$mmk] = $mmv;

                            }

                            $meals['menu'] = ($meal_list);

                            $detail[$key]['meal'] = $meals;
                        }
                    } else {
                        foreach ($item['detail'] as $key => $value) {
                            $meal         = Meal::where('id', $value['meal_id'])->value('detail');
                            $meal_id_list = array_column($meal, 'menu_id');

                            $meal_list = Menu::whereIn('id', $meal_id_list)->select();

                            foreach ($meal_list as $mmk => $mmv) {
                                $menu_detail = $mmv['detail'];
                                foreach ($mmv['detail'] as $k => $v) {
                                    $menu_detail[$k]['type'] = Type::where('id', $v['type_id'])->find();

                                    $menu_detail[$k]['brand'] = Brand::where('id', $v['brand_id'])->find();

                                    $menu_detail[$k]['spec']        = Spec::where('id', $v['spec_id'])->find();
                                    $menu_detail[$k]['old_menu']    = Menu::where('id', $v['menu_id'])->find();
                                    $menu_detail[$k]['old_menu_id'] = $v['menu_id'];

                                }
                                $mmv['detail'] = $menu_detail;
                                //$meal_list[$mmk] = $mmv;

                            }

                            $meals['menu'] = ($meal_list);

                            $detail[$key]['meal'] = $meals;
                        }
                    }

                    $item['detail'] = $detail;
                    return $item;

                });
        } catch (DataNotFoundException | ModelNotFoundException $e) {
            $data = [];
        } catch (DbException $e) {
            throw new ApiServiceException('查询列表失败，信息' . $e->getMessage());
        }

        return $data;
    }
    /**
     * 添加
     * @param $param
     * @return bool
     */
    public function createData($param): bool
    {
        $result = $this->model::create($param);
        return (bool) $result;
    }

    /**
     * 数据详情
     * @param $id
     * @return array
     * @throws ApiServiceException
     */
    public function getDataInfo($id): array
    {
        $data = $this->model->where('id', '=', $id)->findOrEmpty();
        if ($data->isEmpty()) {
            throw new ApiServiceException('数据不存在');
        }
        return $data->toArray();
    }

    /**
     * 修改
     * @param $id
     * @param $param
     * @return bool
     * @throws ApiServiceException
     */
    public function updateData($id, $param): bool
    {
        $data = $this->model->where('id', '=', $id)->findOrEmpty();
        if ($data->isEmpty()) {
            throw new ApiServiceException('数据不存在');
        }
        $result = $data->save($param);

        if (! $result) {
            throw new ApiServiceException('更新失败');
        }

        return true;
    }

    /**
     * 删除
     * @param mixed $id
     * @return bool
     * @throws ApiServiceException
     */
    public function deleteData($id): bool
    {
        $result = $this->model::destroy(function ($query) use ($id) {
            $query->whereIn('id', $id);
        });

        if (! $result) {
            throw new ApiServiceException('更新失败');
        }

        return true;
    }

}
